import userApi from '@/config/server/api/user';

function listByRoles(roleType) {
  let params = {}
  if (roleType) {
    params.roleType = roleType
  }
  return userApi.orgManagement.roleNameList.fetch(params).then(res => {
    return res.map(item => {
      item.key = item.roleId.toString()
      item.value = item.roleName
      return item
    })
  })
}

function listByRoles1() {
  return userApi.orgManagement.roleNameList.fetch().then(res => {
    let roleFiterList = []
    res.forEach((item, index) => {
      if (item.roleType == 1) {
        item.key = item.roleId.toString()
        item.value = item.roleName
        roleFiterList.push(item)
      }
    })
    return roleFiterList
  })


}


let roles = []

function getRoles() {
  return listByRoles1().then(res => {
    roles = res
    return res
  })
}

function getRoles1(roleType) {
  return listByRoles(roleType).then(res => {
    roles = res
    return res
  })
}

function create(opt = {}, roleType) { // key data multiple hid
  return {
    title: opt.title || '角色',
    placeholder: opt.placeholder || '请选择角色',
    key: opt.key,
    disabled: opt.disabled,
    col: opt.col || 24,
    load: (o) => {
      getRoles1(roleType).then(res => {
        o.select.options = res
      })
    },
    select: {
      options: roles,
      multiple: opt.multiple,
      maxTagCount: opt.maxTagCount,
    },
    data: opt.data,
    rules: [{
      required: true,
      type: opt.type,
      message: '请选择角色',
      trigger: 'blur'
    }]
  }
}
export default {
  create,
  listByRoles
}